home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Windows News 2010 Summer - Disc 1
/
WN_Ete2010_CD1.iso
/
Onglet5
/
Weezo
/
Weezo setup.exe
/
{code_appDir}
/
www
/
bittorrentTracker.php
< prev
next >
Wrap
PHP Script
|
2010-05-19
|
5KB
|
138 lines
<?php
/**
* Bittorrent tracker
*
* PHP version 5
*
* LICENSE: This source file is subject to version 3.0 of the PHP license
* that is available through the world-wide-web at the following URI:
* http://www.php.net/license/3_0.txt. If you did not receive a copy of
* the PHP License and are unable to obtain it through the web, please
* send a note to license@php.net so we can mail you a copy immediately.
*
* @category NA
* @package NA
* @author Nicolas Bruley / Peer 2 World <contact@weezo.net>
* @copyright 2005-2007 Nicolas Bruley / Peer 2 World
* @license http://www.php.net/license/3_0.txt PHP License 3.0
* @version CVS: $Id:$
* @link http://www.weezo.net
* @since File available since Release 1.0.8
*/
/**
* @desc debug
*
* @param string $msg: debug message
*/
function out($msg){cfDebugSingle($msg);}
/**
* @desc Return error
*
* @param string $errorCaption: caption to be displayed in bittorrent client
*/
function trackerError($errorCaption){
out('Error : '.$errorCaption.' => '.$_SERVER['REQUEST_URI']);
die('d14:failure reason'.strlen($errorCaption).':'.$errorCaption.'e');
}
out('Tracker request :'.$_SERVER['REQUEST_URI']);
// Check GET parameters (1/2)
if(!isset($_GET['bittorrentPort'])) trackerError('Incorrect Torrent file');
cfDebugSetBuffer('bittorrent',$_GET);
// Correct ill-formated GET address (official bittorrent client...)
if(strpos($_GET['bittorrentPort'],'?')){
$get=substr($_GET['bittorrentPort'],strpos($_GET['bittorrentPort'],'?')+1);
if(strpos($get,'=')) $_GET[substr($get,0,strpos($get,'='))]=substr($get,strpos($get,'=')+1);
$_GET['bittorrentPort']=substr($_GET['bittorrentPort'],0,strpos($_GET['bittorrentPort'],'?'));
}
// Check GET parameters (2/2)
if(!isset($_GET['info_hash'])) trackerError('Incorrect Torrent file');
if(!cfGGetVar('bittorrentEnabled')) trackerError(cfCaption('loginForbiddenAccess'));
if(!isset($_GET['event'])) $_GET['event']='started';
// Retreive info_hash
$infoHash=substr($_SERVER['REQUEST_URI'],strpos($_SERVER['REQUEST_URI'],'info_hash=')+10);
if(strpos($infoHash,'&')) $infoHash=substr($infoHash,0,strpos($infoHash,'&'));
$infoHash=str_replace('-','%2D',str_replace('.','%2E',str_replace('_','%5F',strtoupper($infoHash))));
$infoHash=strtolower(rawurldecode($infoHash));
// Stopped and Completed events
if((isset($_GET['left']) && $_GET['left']==0) || $_GET['event']=='stopped' || $_GET['event']=='completed'){
// Completed download : inform seeder
if($_GET['event']=='completed'){
if($handle=@fsockopen('127.0.0.1',cfGGetVar('bittorrentPort'),$errno,$errstr,4)) {
fwrite($handle,'torrentCompleted'.rawurldecode($infoHash));
fclose($handle);
}
}
$response='d8:completei1e10:incompletei0e8:intervali10800e5:peersld2:ip9:127.0.0.27:peer id20:-WZ0000-fooXXXXXXXXX4:porti7000eeee';
out('event : '.$_GET['event'].' response : '.$response);
// Send "fake" response
header('Content-Type: text/plain');
die($response);
}
// Seek torrent in transfers
require_once(INCLUDE_DIR.'transferFunctions.php');
$transfers=tReadTransfersFiles();
$found=false;
foreach ($transfers as $id=>$tr) {
if($tr->type=='bittorrentDownload' && strtolower(rawurldecode($tr->infoHash))==$infoHash) {$found=true; break;}
}
// If transfer is part of a directLink or a publishDownload, check it's still valid
if($tokenId=$tr->linkedTransferToken){
$tokenList=efTokensRead();
if(!isset($tokenList[$tokenId])) $found=false;
elseif(!efTokenIsValid($tokenList[$tokenId])) $found=false;
unset($tokenList);
}
// Centralized authentication check: inform central server wether torrent exists or not
if(isset($_GET['auth'])) die(($found)?'ok':'invalid');
// Check transfer
if(!$found) trackerError('Nonexistent Torrent');
$extraInfo='';
if($tr->trackerIdSet){
//if(!isset($_GET['trackerid']) || $_GET['trackerid']!=$tr->id) trackerError(cfCaption('loginForbiddenAccess'));
$extraInfo.='10:tracker id'.strlen($tr->id).':'.$tr->id;
if($tr->status==8) trackerError('Torrent error - source files modified');
if($tr->status==4) trackerError('Torrent error - Tranfer completed, cannot re-download');
if($tr->status==6 || $tr->status==9) trackerError('Torrent error - Tranfer canceled');
}
else {
$extraInfo.='10:tracker id'.strlen($tr->id).':'.$tr->id;
$transfers[$tr->id]->trackerIdSet=true;
$needSave=true;
}
// Update transfer status to "connecting"
if($tr->status<3 || $tr->status==5){
$transfers[$tr->id]->status=7;
$needSave=true;
}
// Send response
$ip=str_replace('localhost','127.0.0.1',$_SERVER['SERVER_ADDR']); // Server IP adress // prod
if(strpos($ip,':')) $ip=substr($ip,0,strpos($ip,':'));
//$response='d8:intervali120e12:min intervali120e8:completei1e10:incompletei0e5:peersld2:ip'.strlen($ip).':'.$ip.'7:peer id20:-WZ0000-unRegistered4:porti'.cfGGetVar('bittorrentPort').'eeee';
$response='d8:completei1e10:incompletei0e8:intervali120e12:min intervali120e5:peersld2:ip'.strlen($ip).':'.$ip.'7:peer id20:-WZ0000-unRegistered4:porti'.cfGGetVar('bittorrentPort').'eeee';
header('Content-Type: text/plain');
echo $response;
out('Request OK, reponse : '.$response);
// Save tranfers
if(isset($needSave)) tWritePHPTransfersFile($transfers);
?>